/* The syntax for fsync() is :

#include <unistd.h>
int fsync(int file_descriptor);

*/
/************************************************************************************************/

//A test program that uses fsync()

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#define mega_string_len 250000

main() {
  char *mega_string;
  int  file_descriptor;
  int  ret;
  char fn[]="fsync.file";

  if ((mega_string = (char*) malloc(mega_string_len)) == NULL)
    perror("malloc() error");
  else if ((file_descriptor = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    memset(mega_string, 's', mega_string_len);
    if ((ret = write(file_descriptor,
                     mega_string, mega_string_len)) == -1)
      perror("write() error");
    else {
      printf("write() wrote %d bytes\n", ret);
      if (fsync(file_descriptor) != 0)
        perror("fsync() error");
      else if ((ret = write(file_descriptor,
                            mega_string, mega_string_len)) == -1)
        perror("write() error");
      else
        printf("write() wrote %d bytes\n", ret);
    }
    close(file_descriptor);
    unlink(fn);
  }
}
//the result shoul be : write() wrote 250000 bytes
//                      write() wrote 250000 bytes
